﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marta.buchalska.wesele.DataModel;

namespace Marta.buchalska.wesele
{
    public partial class Gallery : Page
    {
        protected void PhotoList_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var list = (ListView)sender;

                list.DataSource = GetPhoto();
                list.DataBind();
            }
        }

        private IEnumerable<Photo> GetPhoto()
        {
            var galleryName = (string)RouteData.Values["gallery_name"];
            var resoult = new List<Photo_gallery>();
            if (galleryName == null || "default" == galleryName)
            {
                var context = new WeedingDBDataContext();
                if (context.Photo_galleries.FirstOrDefault() == null)
                    return null;
                var firstOrDefault = context.Photo_galleries.FirstOrDefault();
                if (firstOrDefault != null)
                    return firstOrDefault.Photos.ToList();
            }
            else
            {
                var context = new WeedingDBDataContext();
                Photo_gallery firstOrDefault = context.Photo_galleries.FirstOrDefault(d => d.Name == galleryName);
                if (firstOrDefault != null)
                    return firstOrDefault.Photos.ToList();
            }
            return (IEnumerable<Photo>)resoult;
        }
    }
}